﻿using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace M4W.HorizontalTabs
{
    public class HTabsEP : System.Web.UI.WebControls.WebParts.EditorPart
    {
        // Constructor

        public HTabsEP()
        {
            this.Title = ":: M4W Settings";
        }

        // Controls

        protected TextBox ep_m4wList;
        protected DropDownList ep_m4wNrItems;
        protected DropDownList ep_m4wOrder;
        protected TextBox ep_m4wFields;

        // Render Editor Part

        protected override void CreateChildControls()
        {
            this.Controls.Add(new LiteralControl("<br />"));

            ep_m4wList = new TextBox();
            ep_m4wList.Width = 176;
            ep_m4wList.CssClass = "UserInput";
            this.Controls.Add(new LiteralControl("<div class='UserSectionHead'><label title='Enter List Name or URL.'>List Name or URL</label></div>"));
            this.Controls.Add(new LiteralControl("<div class='UserSectionBody'><div class='UserControlGroup'><nobr>"));
            this.Controls.Add(ep_m4wList);
            this.Controls.Add(new LiteralControl("</nobr></div>"));
            this.Controls.Add(new LiteralControl("</div>"));

            ep_m4wNrItems = new DropDownList();
            ep_m4wNrItems.Width = 92;
            ep_m4wNrItems.CssClass = "UserInput";
            ep_m4wNrItems.Items.Add("1"); ep_m4wNrItems.Items.Add("2"); ep_m4wNrItems.Items.Add("3"); ep_m4wNrItems.Items.Add("4"); ep_m4wNrItems.Items.Add("5"); ep_m4wNrItems.Items.Add("6"); ep_m4wNrItems.Items.Add("7"); ep_m4wNrItems.Items.Add("8"); ep_m4wNrItems.Items.Add("9"); ep_m4wNrItems.Items.Add("10");
            this.Controls.Add(new LiteralControl("<div class='UserSectionHead'><label title='Select Number of Tabs.'>Number of Tabs</label></div>"));
            this.Controls.Add(new LiteralControl("<div class='UserSectionBody'><div class='UserControlGroup'><nobr>"));
            this.Controls.Add(ep_m4wNrItems);
            this.Controls.Add(new LiteralControl("</nobr></div>"));
            this.Controls.Add(new LiteralControl("</div>"));

            ep_m4wOrder = new DropDownList();
            ep_m4wOrder.Width = 176;
            ep_m4wOrder.CssClass = "UserInput";
            ep_m4wOrder.Items.Add("By ID Ascending"); ep_m4wOrder.Items.Add("By ID Descending"); ep_m4wOrder.Items.Add("By Modified Ascending"); ep_m4wOrder.Items.Add("By Modified Descending");
            this.Controls.Add(new LiteralControl("<div class='UserSectionHead'><label title='Choose Sort Order.'>Sort Order</label></div>"));
            this.Controls.Add(new LiteralControl("<div class='UserSectionBody'><div class='UserControlGroup'><nobr>"));
            this.Controls.Add(ep_m4wOrder);
            this.Controls.Add(new LiteralControl("</nobr></div>"));
            this.Controls.Add(new LiteralControl("</div>"));
                     
            ep_m4wFields = new TextBox();
            ep_m4wFields.Width = 176;
            ep_m4wFields.CssClass = "UserInput";
            this.Controls.Add(new LiteralControl("<div class='UserSectionHead'><label title='Enter List Fields.'>List Fields</label></div>"));
            this.Controls.Add(new LiteralControl("<div class='UserSectionBody'><div class='UserControlGroup'><nobr>"));
            this.Controls.Add(ep_m4wFields);
            this.Controls.Add(new LiteralControl("</nobr></div>"));
            this.Controls.Add(new LiteralControl("<script>if(m4w){m4w.xC();}</script><br /><br />"));
            this.Controls.Add(new LiteralControl("</div>"));

            base.CreateChildControls();
            this.ChildControlsCreated = true;
        }

        // Save Web Part Parameters

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            HTabsWP wp = (HTabsWP)this.WebPartToEdit;
            if (wp != null)
            {
                wp.m4wListUrl = ep_m4wList.Text;
                wp.m4wNrItems = ep_m4wNrItems.Text;
                wp.m4wOrder = ep_m4wOrder.Text;
                wp.m4wFields = ep_m4wFields.Text;
            }
            return true;
        }

        // Read Web Part Parameters

        public override void SyncChanges()
        {
            EnsureChildControls();
            HTabsWP wp = (HTabsWP)this.WebPartToEdit;
            if (wp != null)
            {
                ep_m4wList.Text = wp.m4wListUrl;
                ep_m4wNrItems.Text = wp.m4wNrItems;
                ep_m4wOrder.Text = wp.m4wOrder;
                ep_m4wFields.Text = wp.m4wFields;
            }
            return;
        }
    }
}
